<?php

namespace App\Http\Controllers\Admin;

use App\Exceptions\AppException;
use App\Models\Danmu;
use App\Models\Data;
use App\Models\Member;
use App\Models\Title;
use App\Models\Prefix;
use App\Services\ExcelExportService;
use Illuminate\Http\Request;

class TitleController extends BaseController {

    public function addTitle(Request $request)
    {
        $params = $request->all();

        $result = Title::query()->create([
            'title_name' => $params['title_name'],
            'link' => $params['title_name'],
            'img_title' => $params['img_title'],
            'img_main' => $params['img_main'],
            'img_secondary' => $params['img_secondary'],

        ]);
        return $this->success($result, '添加成功');
    }
    public function updateTitle($id, Request $request)
    {
        $params = $request->all();
        $titleData = Title::query()->where('title_id', $id)->first();
        if (!$titleData) {
            return $this->errorCode('10001', [], '该id不存在。请检查id是否正确');
        }

        $result = Title::query()->where('title_id', $id)->update([
            'title_name' => $params['title_name'],
            'link' => $params['link'],
            'img_title' => $params['img_title'],
            'img_main' => $params['img_main'],
            'img_secondary' => $params['img_secondary'],

        ]);
        return $this->success([], '修改成功');
    }

    public function detailTitle($id, Request $request)
    {
        $result = Title::query()->where('title_id', $id)->first();
        return $this->success($result, '添加成功');
    }

    public function listTitle(Request $request)
    {
        $params = $request->all();
        $result = Title::query()->get();
        return $this->success($result, '获取成功');
    }

    public function deleteTitle($id)
    {
        $titleData = Title::query()->where('title_id', $id)->first();
        if (!$titleData) {
            return $this->errorCode('10001', [], '该id不存在。请检查id是否正确');
        }

        $result = Title::query()->where('title_id', $id)->delete();
        return $this->success([], '修改成功');
    }
}